package com.lzp.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import com.utils.SizeUtils

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 4/13/21
 */
class PathText(context: Context?, attrs: AttributeSet? = null) : View(context, attrs) {

    private val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val path = Path()

    init {
        paint.color = Color.RED
        paint.textSize = SizeUtils.dip2px(context, 22f)
        paint.textAlign = Paint.Align.CENTER
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (w != 0 && h != 0) {
            path.moveTo(w / 4f, h / 2f)
           /* path.cubicTo(w / 4f, h / 2f,
                    w / 2f, h / 3f,
                    w / 4f * 3, h / 2f)*/

            path.addCircle(w/2f,h/2f,w/6f,Path.Direction.CW)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            rotate(90f,width/2f,height/2f)
            drawTextOnPath("天行健，君子以自强不息", path, 0f, 0f, paint)
        }
    }
}